package demo3;

//class Student implements Comparable<Student>{
//    int age ;
//    String name ;
//    void func(){
//
//    }
//
//    public Student(int age, String name) {
//        this.age = age;
//        this.name = name;
//    }
//
//    @Override
//    public int compareTo(Student o) {
//        return this.name.compareTo(o.name) ;
//    }
//
//    @Override
//    public int hashCode() {
//        return super.hashCode();
//    }
//}
//
//
//public class Test3 {
//    public static void main(String[] args) {
//         Student student1 = new Student(20,"ls") ;
//         Student student2 = new Student(18,"ls") ;
//         if(student1.compareTo(student2) > 0){
//             System.out.println("zs");
//         }else {
//             System.out.println("ls");
//         }
//    }
//}

import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

class Student{
    int age ;
    String name ;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

public class Test3 {
    static  void bubbleSort(Student[] students){
        AgeComparator ageComparator = new AgeComparator() ;
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - i - 1; j++) {
                if(ageComparator.compare(students[j],students[j+ 1]) > 0){
                    Student tmp = students[j] ;
                    students[j] = students[j + 1] ;
                    students[j+1] = tmp ;
                 }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students = {new Student(14,"张三"),
                new Student(18,"李四"),
                new Student(15,"王五"),
                new Student(20,"赵六"),
                new Student(22,"王八"),};
     bubbleSort(students);
        for(Student student : students){
            System.out.println(student);
        }
    }


}